//
//  HWRecyclingRecordsViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2021/7/21.
//  Copyright © 2021 Chendaobin. All rights reserved.
//

#import "HWRecyclingRecordsViewController.h"
#import "HWRecyclingTableViewCell.h"
#import "HWRecyclingRecordsTableViewCell.h"
@interface HWRecyclingRecordsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)NSMutableArray *dataMuArr;
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSString *p; //分页
@property(strong,nonatomic) NSString *max_p; // 最大分页

@property(strong,nonatomic) UIImageView *defaultIamegView;
@property(strong,nonatomic) UILabel *wuLabel;
@end

@implementation HWRecyclingRecordsViewController
-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {


        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor =RGB(255, 255, 255);
        _mainaTableVie.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;


    }
    return   _mainaTableVie ;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self addrefreshControl];
    
    [self zanWuNoDdataIcoImageView];
    
   
}



- (void)zanWuNoDdataIcoImageView {
    
    self.defaultIamegView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-189)/2,(kScreenH - 127)/2-15-80, 189, 124)];
    self.defaultIamegView.image = [UIImage imageNamed:@"details_dealbg_NOdata"];
    [self.mainaTableVie addSubview: self.defaultIamegView];
    
    
    self.wuLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.defaultIamegView.bottom, kScreenW, 15)];
    self.wuLabel.text = @"暂无数据";
    self.wuLabel.font = [UIFont systemFontOfSize:13];
    self.wuLabel.textColor = RGB(102, 102, 102);
    self.wuLabel.textAlignment  = NSTextAlignmentCenter;
    [self.mainaTableVie addSubview: self.wuLabel];
    
    
    
    
}



- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self.mainaTableVie.mj_header beginRefreshing]; 
    
}

// 设置UI
- (void)setupUI {
    
    self.navigationItem.title = @"回收记录";
    self.view.backgroundColor = RGB(247, 247, 247);
    
    self.mainaTableVie.delegate = self;
    self.mainaTableVie.dataSource = self;
    [self.view addSubview:self.mainaTableVie];
    [self.mainaTableVie mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(NavBarHeight);
    }];
    
    
}

// 添加刷新
- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    [self.mainaTableVie.mj_header beginRefreshing];
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
      
    // 全部评论
    
}


// 发送网路请求
- (void)underRefresh {
    
    self.p = @"1";
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"recover_order",
                                @"ios" : @"1",
                                @"uid" :userID,
                                @"t" :tStr,
                                @"p":self.p,
                                @"sign": signMD5
                                };
    
    
   //  [MBProgressHUD show:@"正在刷新"];
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
      
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
     //   [MBProgressHUD hideHUD];
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        if ([codeStr intValue] == 1) {
         

            self.dataMuArr = [HWhuishouModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            [self.mainaTableVie reloadData];
            
            if (self.dataMuArr.count>0) {
                
                self.defaultIamegView.hidden = YES;
                self.wuLabel.hidden = YES;
               //  self.mainaTableVie.backgroundColor = RGB(255, 255, 255);
                self.mainaTableVie.backgroundColor = RGB(247, 247, 247);
                
            } else {
                
                self.mainaTableVie.backgroundColor = RGB(255, 255, 255);
                
            }

        } else {
            
             // [MBProgressHUD  showError:@"加载失败"];
            
 
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
      
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
    }];
    
    
    
}


- (void)onRefresh  {
    
    
    if ([self.p intValue] > [self.max_p intValue] ) {
         
        [MBProgressHUD showError:@"请稍后在加载数据"];
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        return ;
    }
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"recover_order",
                                @"ios" : @"1",
                                @"uid" :userID,
                                @"t" :tStr,
                                @"p":self.p,
                                @"sign": signMD5
                                };
    
    
   //  [MBProgressHUD show:@"正在刷新"];
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
      
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
     //   [MBProgressHUD hideHUD];
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        if ([codeStr intValue] == 1) {
         

           NSMutableArray *arr = [HWhuishouModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
          
            for (int i = 0 ; i < arr.count; i++) {
                [self.dataMuArr addObject:arr[i]];
                
                
            }
            
            
            
            self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            
            [self.mainaTableVie reloadData];
            

        } else {
            
             // [MBProgressHUD  showError:@"加载失败"];
            
 
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
      
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
    }];
    
    
    
    
    

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
  
    
    
    return  self.dataMuArr.count ;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {
 
    
    return  89;
    
    
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

        
        static NSString  *cellID = @"cellID";
        
    HWRecyclingRecordsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"HWRecyclingRecordsTableViewCell" owner:nil options:nil].firstObject;
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        HWhuishouModel   *model =  self.dataMuArr[indexPath.row];
        cell.model = model;
        
 
        
        
        return  cell;
        
 
    
    



}

@end
